package com.lagou.jdbc02;

import com.lagou.utils.JDBCUtils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author wsl
 * @Date 2021/8/18 21:19
 * @Project_Name mysql
 */
public class TestDML {

    /**
     * 插入操作
     */
    @Test
    public void testInsert() throws SQLException {
        //1.通过JDBCUtils工具类 获取连接
        Connection con = JDBCUtils.getConnection();

        //2.获取Statement对象
        Statement statement = con.createStatement();
        //2.1 编写SQL
        String sql = "insert into jdbc_user values(null,'张百万','123','2020/1/1')";
        //2.2 执行sql
        int i = statement.executeUpdate(sql);
        System.out.println(i);

        //3.关闭流
        JDBCUtils.close(con, statement);
    }
    /**
     * 更新操作，根据id修改用户名
     */
        @Test
        public void testUpdate() throws SQLException {

            Connection con = JDBCUtils.getConnection();

            Statement statement = con.createStatement();

            String sql = "update jdbc_user set username='刘能' where id=1";
            int i = statement.executeUpdate(sql);
            System.out.println(i);

            JDBCUtils.close(con, statement);
        }
    /**
     * 删除操作，删除id 为5，6的数据
     */
    @Test
    public void testDelete() throws SQLException {

        Connection con = JDBCUtils.getConnection();

        Statement statement = con.createStatement();

        String sql = "delete from jdbc_user where id in(5,6)";
        int i = statement.executeUpdate(sql);
        System.out.println(i);

        JDBCUtils.close(con,statement);
    }

}
